﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

namespace DAL
{
    public class DCustomer : BaseProvider
    {
        public void InsertCustomer(string sBranch, string sFullName, int sNRIC, string sDateBirth, int sGender, int sCity, int sEducation, string sOtherEdu)
        {
            try
            {
                string sSQLCommand = "insert into T_CUSTOMER([FullName],[NRIC],[DateBirth],[Gender],[BranchId],[City],[EducationLevel],[OtherEducation])VALUES(";
                sSQLCommand += "'" + sFullName + "'," + sNRIC + ",'" + Convert.ToDateTime(sDateBirth) + "'," + sGender + "," + sBranch + "," + sCity + "," + sEducation + ",'" + sOtherEdu + "')";
                DbCommand cmd = Provider.GetSqlStringCommand(sSQLCommand);
                Provider.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            { 

            }
        }

        public DataSet GetCustomer(int iPageNo, int iPageSize, out int iTotalRows)
        {
            DataSet dsCus = new DataSet();

            try
            {

                DbCommand cmd = Provider.GetStoredProcCommand("USP_GetCustomerPage");

                Provider.AddInParameter(cmd, "@iPageNo", SqlDbType.Int, iPageNo);
                Provider.AddInParameter(cmd, "@iPageSize", SqlDbType.Int, iPageSize);
                Provider.AddOutParameter(cmd, "@iTotalRows", SqlDbType.Int, 8);

                dsCus = Provider.ExecuteDataSet(cmd);
                if (dsCus.Tables[0].Rows.Count > 0)
                    iTotalRows = Convert.ToInt32(Provider.GetParameterValue(cmd, "@iTotalRows"));
                else
                    iTotalRows = 0;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dsCus;
        }

        public bool checkNRIC(int iNRIC)
        {
            bool bRes = false;
            try
            {
                string sSQLCommand = "select count(1) from T_CUSTOMER where NRIC = @nric";
                DbCommand cmd = Provider.GetSqlStringCommand(sSQLCommand);
                Provider.AddInParameter(cmd, "@nric",SqlDbType.Int,iNRIC);
                bRes = Convert.ToInt32(Provider.ExecuteScalar(cmd)) > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return bRes;
        }


    }
}

